#!/bin/bash

CHANGED=0

IFNAMEEXISTS=`cat /boot/cmdline.txt | grep "net.ifnames=0"`
if [ "$IFNAMEEXISTS" == "" ]; then
  mount -o remount,rw /boot
  echo "Enabling old-style network interface naming"
  sed -i "s/\$/\ net.ifnames=0/g" /boot/cmdline.txt
  CHANGED=1
fi

# ntp user
USEREXISTS=`cat /etc/passwd | grep ntp`
if [ "$USEREXISTS" == "" ]; then
  addgroup -S ntp
  adduser -G ntp -h / -H -S -D ntp -g "NTP server"
  CHANGED=1
fi

# Re-link installed extensions
if [ -f /opt/hifiberry/bin/extensions ]; then
  /opt/hifiberry/bin/extensions link
  systemctl enable --now extensions
fi
 
# Script should run only once
mv $0 $0.done

if [ "$CHANGED" != "0" ]; then
  sync
  reboot
fi
